﻿using System;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Stock.Model;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Stock.Core.MongoDB;
using Stock.Core.Common;

namespace Stock.Core
{
    public static class InitModelExtensions
    {

        public static ContainerBuilder UseStockCore(this ContainerBuilder builder, Func<bool> useMongoDB)
        {
            var assembly = Assembly.Load(new AssemblyName("Stock.Core"));
            builder.RegisterAssemblyTypes(assembly)
               .Where(t => t.Name.EndsWith("Service"))
               .AsImplementedInterfaces()
                  .InstancePerLifetimeScope();

            //builder.RegisterType<RedisProvider>().As<IRedisProvider>().InstancePerLifetimeScope();

            builder.RegisterGeneric(typeof(MongoBaseRepository<>)).As(typeof(IMongoBaseRepository<>))
    .InstancePerLifetimeScope();

            if (useMongoDB())
            {
                builder.RegisterType<MongoLoggerService>().As<ILoggerService>().InstancePerLifetimeScope();
            }
            else
            {
                builder.RegisterType<LoggerService>().As<ILoggerService>().InstancePerLifetimeScope();
            }


            return builder;
        }
    }

}
